package dnl.toolbox.time;

public class Stopwatch {

	private long t1;
	private long t2;

	public Stopwatch() {
		super();
	}

	public Stopwatch createAndStart(){
		Stopwatch stopwatch = new Stopwatch();
		stopwatch.start();
		return stopwatch;
	}
	
	public void start() {
		if (t1 != 0) {
			throw new IllegalArgumentException("Cannot start this stopwatch twice!");
		}
		t1 = System.currentTimeMillis();
	}

	public void stop() {
		if (t2 != 0) {
			throw new IllegalArgumentException("Cannot stop this stopwatch twice!");
		}
		t2 = System.currentTimeMillis();
	}

	public long getElapsedTime() {
		if (t1 == 0 || t2 == 0) {
			throw new IllegalArgumentException(
					"Usage: call start() and stop() and only then getElapsedTime()");
		}
		return t2 - t1;
	}

	public String getFormattedElapsedTime() {
		return TimeUtils.formatElapsedTime(getElapsedTime());
	}
}
